﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;

namespace Web_Oto.Bases
{
    public class PortalCache
    {
        private DateTime m_dtmExpiration;
        private int m_intStatus;
        private object m_objCacheValue;

        // Methods
        public static object GetCacheItem(string strCacheName)
        {
            try
            {
                object obj2 = HttpContext.Current.Cache.Get(strCacheName);
                if (obj2 != null)
                {
                    PortalCache cache = (PortalCache)obj2;
                    int num = (ConfigurationManager.AppSettings["CacheWarning"] != null) ? Convert.ToInt32(ConfigurationManager.AppSettings["CacheWarning"].ToString()) : -5;
                    if (cache != null)
                    {
                        if (((cache.CacheStatus == 1) && (cache.ExpirationDate != DateTime.MinValue)) && (cache.ExpirationDate.AddMinutes((double)num).Ticks <= DateTime.Now.Ticks))
                        {
                            cache.CacheStatus = 2;
                            HttpContext.Current.Cache.Insert(strCacheName, cache, null, cache.ExpirationDate, TimeSpan.Zero);
                            return null;
                        }
                        return cache.CacheValue;
                    }
                }
            }
            catch
            {
                return null;
            }
            return null;
        }

        public static DateTime GetDateTimeCacheItem(string strCacheName)
        {
            object cacheItem = GetCacheItem(strCacheName);
            if (cacheItem != null)
            {
                return (DateTime)cacheItem;
            }
            return DateTime.MinValue;
        }

        public static int GetIntCacheItem(string strCacheName)
        {
            object cacheItem = GetCacheItem(strCacheName);
            if (cacheItem != null)
            {
                return (int)cacheItem;
            }
            return -1;
        }

        public static string GetStringCacheItem(string strCacheName)
        {
            object cacheItem = GetCacheItem(strCacheName);
            if (cacheItem != null)
            {
                return (string)cacheItem;
            }
            return "";
        }

        public static bool SetCacheItem(string strCacheName, object objCacheValue)
        {
            try
            {
                PortalCache cache = new PortalCache
                {
                    CacheValue = objCacheValue,
                    CacheStatus = 1
                };
                HttpContext.Current.Cache.Insert(strCacheName, cache);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool SetCacheItem(string strCacheName, object objCacheValue, DateTime dtmExpiration)
        {
            PortalCache cache = new PortalCache
            {
                ExpirationDate = dtmExpiration,
                CacheValue = objCacheValue,
                CacheStatus = 1
            };
            HttpContext.Current.Cache.Insert(strCacheName, cache, null, cache.ExpirationDate, TimeSpan.Zero);
            return true;
        }

        // Properties
        public int CacheStatus
        {
            get
            {
                return this.m_intStatus;
            }
            set
            {
                this.m_intStatus = value;
            }
        }

        public object CacheValue
        {
            get
            {
                return this.m_objCacheValue;
            }
            set
            {
                this.m_objCacheValue = value;
            }
        }

        public DateTime ExpirationDate
        {
            get
            {
                return this.m_dtmExpiration;
            }
            set
            {
                this.m_dtmExpiration = value;
            }
        }

        // Nested Types
        public enum CacheStatusEnum
        {
            Ok = 1,
            SoonToBeExpired = 2
        }

    }
}
